package cn.pegasus.teamwork.appinterface.controller.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import cn.iyizhan.teamwork.base.util.ObjectUtil;
import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.appinterface.base.BaseController;
import cn.pegasus.teamwork.appinterface.controller.washer.vo.ReturnOrderVo;
import cn.pegasus.teamwork.appinterface.util.DateUtils;
import cn.pegasus.teamwork.appinterface.util.ParameterEunm;
import cn.pegasus.teamwork.appinterface.util.ReturnMsgTips;
import cn.pegasus.teamwork.appinterface.util.ReturnRsUtils;
import cn.pegasus.teamwork.pegasus.entity.BussServiceProject;
import cn.pegasus.teamwork.pegasus.entity.BussUcenterArea;
import cn.pegasus.teamwork.pegasus.entity.BussUserCars;
import cn.pegasus.teamwork.pegasus.entity.BussUserComment;
import cn.pegasus.teamwork.pegasus.entity.BussUserConsumorder;
import cn.pegasus.teamwork.pegasus.entity.BussUserOrder;
import cn.pegasus.teamwork.pegasus.entity.BussWasherInfo;
import cn.pegasus.teamwork.pegasus.service.BussServiceProjectService;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussUserCarsService;
import cn.pegasus.teamwork.pegasus.service.BussUserCommentService;
import cn.pegasus.teamwork.pegasus.service.BussUserConsumorderService;
import cn.pegasus.teamwork.pegasus.service.BussUserOrderService;
import cn.pegasus.teamwork.pegasus.service.BussWasherInfoService;
import cn.pegasus.teamwork.sysconfig.entity.SysConfigDictionary;
import cn.pegasus.teamwork.sysconfig.service.SysConfigDictionaryService;
import cn.pegasus.teamwork.sysconfig.service.SysConfigMessageService;
import cn.pegasus.teamwork.util.DateCompareUtil;
import cn.pegasus.teamwork.util.LatitudeLontitudeUtil;

/**
 * @ClassorderName: SysUcenerUserController
 * @Description: 用户控制中心
 * @author ryan.bian
 * @date 2017年11月12日 上午10:10:32
 * 
 *       包括以下主要几个接口：
 * 
 *       =================================
 * 
 *       1.用户购买套餐 2.用户创建定单 3.用户充值 4.用户评价 5.用户打赏 6.用户订单列表查询 7.用户退单 - RefundVo
 *       8.获取用户定位地址后的小区列表（方圆 500 米）- VillageVo 9.订单详情
 * 
 *       ===========================================
 * 
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
@RequestMapping("/ord/*")
public class UserOrderController extends BaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserOrderController.class);

	/**
	 * 地区service
	 */
	@Autowired
	private BussUcenterAreaService bussUcenterAreaService;

	/**
	 * 地区service
	 */
	@Autowired
	private BussUserOrderService bussUserOrderService;

	/**
	 * 用户消费订单表service
	 */
	@Autowired
	private BussUserConsumorderService consumorderService;

	/**
	 * 车辆管理
	 */
	@Autowired
	private BussUserCarsService bussUserCarsService;

	@Autowired
	private BussUserCommentService commentService;

	@Autowired
	private SysConfigDictionaryService dictionaryService;

	@Autowired
	private BussUcenterAreaService areaService;

	@Autowired
	private BussServiceProjectService projectService;

	@Autowired
	private BussUcenterAreaService ucenterAreaService;

	@Autowired
	private BussWasherInfoService washerInfoService;

	@Autowired
	private SysConfigMessageService messageService;
	
	/**
	 * 用户在下单时，选择可用的时间区域
	 * 
	 * @param userorderName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cts", method = RequestMethod.POST)
	public Map<String, Object> choiceTimes(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "projectId", "areaId", "reservationDate" },
					map);
			int userId = Integer.valueOf(getSessionUserId().toString());
			map.put("userId", userId);
			// 不强制传参数 ： orderDesc[订单描述] areaAddress[预约服务地址] \
			// carImages[车辆图片]
			if (isNotNull) {
				// 0-成功；1-用户订单不存在；2-账户余额不足 ； 3- 分配订单失败; -1-失败
				Map<String, Object> rsmap = bussUserOrderService.choiceTimes(map);
				if (rsmap != null && rsmap.size() > 0) {
					if (rsmap.get("cannot") != null && rsmap.get("sure") != null) {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NOT_EXIST_WASHER_CODE, null);
					} else {
						Map<String, Object> mape = new HashMap<String, Object>();
						mape.put("lis", rsmap);
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, mape);
					}
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NOT_EXIST_WASHER_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 用户创建订单,支持微信支付和余额支付两种方式
	 * 
	 * @param userorderName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/co", method = RequestMethod.POST)
	public Map<String, Object> createOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(
					new String[] { "orderName", "orderBalance", "areaId", "carCode", "carBrand", "carColor",
							"bespokeBeginTime", "bespokeEndTime", "objectsId", "linkMan", "linkTel", "tradeType" },
					map);
			int userId = Integer.valueOf(getSessionUserId().toString());
			map.put("userId", userId);
			// 不强制传参数 ： orderDesc[订单描述] areaAddress[预约服务地址] \ carImages[车辆图片]
			if (isNotNull) {
				// 微信支付
				String orderNo = bussUserOrderService.createPurchaseOrders(map);
				if (!StringUtils.isEmpty(orderNo)) {
					Map<String, Object> tMap = new HashMap<String, Object>();
					tMap.put("orderNo", orderNo);
				
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, tMap);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
				}

			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		return resultMap;
	}

	/**
	 * 只针对用户使用微信进行支付的付款 用户支付已经创建的订单 获取到订单信息，更新订单支付状态，变更用户资金明细，分配订单给洗车工
	 * 
	 * @param userorderName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/pwo", method = RequestMethod.POST)
	public Map<String, Object> payWxOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "orderNo", "payType" }, map);
			int userId = Integer.valueOf(getSessionUserId().toString());
			map.put("userId", userId);
			// 不强制传参数 ： orderDesc[订单描述] areaAddress[预约服务地址] \ carImages[车辆图片]
			if (isNotNull) {
				// 0-成功；1-用户订单不存在；2-账户余额不足 ； 3- 分配订单失败; -1-失败
				Map<String, Object> tMap = bussUserOrderService.payWxOrder(map);
				if (tMap != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, tMap);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PAY_NOTFIND, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 只针对用户使用余额进行支付的付款 用户支付已经创建的订单 获取到订单信息，更新订单支付状态，变更用户资金明细，分配订单给洗车工
	 * 
	 * @param userorderName
	 * @param password
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/po", method = RequestMethod.POST)
	public Map<String, Object> payOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "orderNo", "userPaypwd" }, map);
			int userId = Integer.valueOf(getSessionUserId().toString());
			map.put("userId", userId);
			// 不强制传参数 ： orderDesc[订单描述] areaAddress[预约服务地址] \ carImages[车辆图片]
			if (isNotNull) {
				// 0-成功；1-用户订单不存在；2-账户余额不足 ； 3- 分配订单失败; -1-失败
				int rs = bussUserOrderService.payAccountOrder(map);
				if (rs == 0) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else if (rs == 4) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.EMPET_PAY_PASSWORD, null);
				} else if (rs == 1) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				} else if (rs == 2) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_CREDIT_LOW, null);
				} else if (rs == 5) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PAY_PASSWORD, null);
				}else if (rs == 3) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.EMPET_NOT_WASHER, false);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_403_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 用户充值订单生成
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cro", method = RequestMethod.POST)
	public Map<String, Object> createrechargeOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id",  "type" }, map);
			// 1. 用户充值
			if (isNotNull) {
				int userId = Integer.valueOf(getSessionUserId().toString());
				map.put("userId", userId);
				if (Integer.valueOf(map.get("type").toString()) == 1) {
					Map<String, Object> tMap = consumorderService.createUserRechage(map, 1, 1);
					if (tMap != null) {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, tMap);
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
					}
				} else if (Integer.valueOf(map.get("type").toString()) == 2) {
					// TODO APP充值再定
					// Map<String, Object> tMap = new HashMap<String, Object>();
					// 2. 对于用户充值后，对资金进行补充
					Map<String, Object> twMap = consumorderService.createUserRechageForApp(map, 2, 1);
					if (twMap != null) {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, twMap);
					} else {
						resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
					}

				}

			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("修改用户失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 用户充值列表
	 * 
	 * @param id
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/rgl", method = RequestMethod.POST)
	public Map<String, Object> rechargeList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "userType" }, map);
			// 1. 用户充值
			if (isNotNull) {
				int userId = Integer.valueOf(getSessionUserId().toString());
				map.put("userId", userId);
				map.put("orderType", 1);
				List<BussUserConsumorder> bcos = consumorderService.query(map);
				if (bcos != null && bcos.size() > 0) {
					Map<String, Object> mape = new HashMap<String, Object>();
					mape.put("lis", bcos);
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, mape);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("修改用户失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 用户评价
	 * 
	 * @param phone_number
	 * @param app_code
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/ct", method = RequestMethod.POST)
	public Map<String, Object> comment(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "orderId", "score", "descs" }, map);
			// images 、fid 不是必须字段
			// 1. 用户充值
			if (isNotNull) {
				long userId = getSessionUserId();
				BussUserComment bus = new BussUserComment();
				bus.setUserId(userId);
				bus.setOrderId(Integer.valueOf(map.get("orderId").toString()));
				bus.setScore(Integer.valueOf(map.get("score").toString()));
				bus.setDescs(map.get("descs").toString());
				if (map.get("images") != null) {
					bus.setImages(map.get("images").toString());
				}

				if (map.get("fid") != null) {
					bus.setFid(Integer.valueOf(map.get("fid").toString()));
				}
				bus.setCreateUserId(userId);
				bus.setCreateTime(DateUtils.getNowDate());

				BussUserComment cbus = commentService.add(bus);
				if (cbus != null) {
					// 更新订单状态：
					int id = Integer.valueOf(map.get("orderId").toString());
					Map<String, Object> tMap = new HashMap<String, Object>();
					tMap.put("id", id);
					BussUserOrder bo = bussUserOrderService.detail(tMap);
					if (bo != null) {
						bo.setOrderIsevaluate(1);
						bussUserOrderService.update(bo);
					}

					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, false);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("验证手机号码失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 用户打赏订单-微信支付
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/rro", method = RequestMethod.POST)
	public Map<String, Object> creatRrewardOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "orderBalance", "orderNo", "payType" }, map);
			if (isNotNull) {
				// 支付类型（1-微信支付；2-余额支付）
				Map<String, Object> tMap = bussUserOrderService.createRrewardOrder(map);
				if (tMap != null) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, tMap);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
				}

			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			LOGGER.error("验证手机号码失败.", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 支付用户打赏订单信息(只支持余额支付)
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/pro", method = RequestMethod.POST)
	public Map<String, Object> payRewardOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "orderBalance", "orderNo", "userPaypwd" },
					map);
			int userId = Integer.valueOf(getSessionUserId().toString());
			map.put("userId", userId);
			// 不强制传参数 ： orderDesc[订单描述] areaAddress[预约服务地址] \ carImages[车辆图片]
			if (isNotNull) {
				// 0-成功；1-用户订单不存在；2-账户余额不足 ； 3- 分配订单失败; -1-失败
				int rs = bussUserOrderService.payAccountRrewardOrder(map);
				if (rs == 0) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else if (rs == 4) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.EMPET_PAY_PASSWORD, null);
				} else if (rs == 1) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				} else if (rs == 2) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_CREDIT_LOW, null);
				} else if (rs == 5) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PAY_PASSWORD, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}

		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		return resultMap;
	}

	/**
	 * 用户订单列表查询
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/ols", method = RequestMethod.POST)
	public Map<String, Object> orderLists(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		// 是否为空参数校验
		// 订单状态(1-未开始；2-开始服务；3-服务完成；10-订单取消；20-订单退款;30-已结算)
		map.put("userId", getSessionUserId());
		if (map.get("pageData") == null) {
			map.put("pageData", 10);
		}
		if (map.get("curPage") == null) {
			map.put("curPage", 1);
		}

		// 用户 （-1- 待付款；1-待服务；2-服务中；3-已完成；10-已取消；20-退款中;21-已退款）
		if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == -1) {
			map.put("payStatus", 1);
			map.put("orderStatus", -1);
			map.put("bespokeBeginTimeBegin", "1000");
		} else if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == 1) {
			map.put("payStatus", 2);
			map.put("orderStatus", 1);
		} else if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == 2) {
			map.put("payStatus", 2);
			map.put("orderStatus", 2);
		} else if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == 3) {
			map.put("orderStatus", 3);
			map.put("payStatus", 2);
		} else if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == 10) {
			map.put("orderStatus", 10);
			map.put("payStatus", 2);
		} else if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == 20) {
			map.put("orderStatus", 20);
			map.put("payStatus", 2);
		} else if (map.get("orderStatus") != null && Integer.valueOf(map.get("orderStatus").toString()) == 21) {
			map.put("orderStatus", 21);
			map.put("payStatus", 2);
		}

		PageObject<BussUserOrder> newuses = bussUserOrderService.PageList(map);
		if (newuses != null) {
			// 考虑将小区信息后期放入缓存
			List<BussUserOrder> lis = newuses.getDataList();
			List<ReturnOrderVo> vos = new ArrayList<ReturnOrderVo>();
			if (lis != null && lis.size() > 0) {
				for (BussUserOrder bo : lis) {
					ReturnOrderVo vo = new ReturnOrderVo();
					String projectId = bo.getProjectId();
					String prpjectName = projectService.findProjectName(Long.valueOf(projectId));
					if (!StringUtils.isEmpty(prpjectName)) {
						vo.setProjectName(prpjectName);
					} else {
						vo.setProjectName("");
					}

					// 查询洗车工信息
					
					if(bo.getWasherId() != null){
						int washerId = bo.getWasherId();
						if (washerId > 0) {
							Map<String, Object> rm = new HashMap<String, Object>();

							rm.put("id", washerId);
							BussWasherInfo info = washerInfoService.detail(rm);
							if (info != null) {
								vo.setWahserMobile(info.getWasherMobile());
								vo.setWasherName(info.getWasherRealname());
							} else {
								vo.setWahserMobile("");
								vo.setWasherName("");
							}
						}
					} else {
						vo.setWahserMobile("");
						vo.setWasherName("");
					}

					vo.setId(bo.getId().toString());
					vo.setUserId(bo.getUserId().toString());

					vo.setOrderNo(bo.getOrderNo());// 订单流水号
					vo.setOrderBalance(bo.getOrderBalance().toString());// 金额
					vo.setAreaAddress(bo.getAreaName() + bo.getAreaAddress());
					vo.setCarCode(bo.getCarCode()); // 车牌号
					vo.setCarBrand(bo.getCarBrand());
					// 车辆品牌
					vo.setCarColor(bo.getCarColor());
					// 车辆颜色
					vo.setCarImages(bo.getCarImages());
					// 车辆图片
					vo.setBespokeTime(DateCompareUtil.getDateToString(bo.getBespokeBeginTime()) + "-"
							+ DateCompareUtil.getDateToString(bo.getBespokeEndTime()));
					vo.setLinkMan(bo.getLinkMan());
					// 联系人
					vo.setLinkTel(bo.getLinkTel());
					vo.setPayStatus(bo.getPayStatus());
					// 联系电话
					vo.setOrderStatus(bo.getOrderStatus().toString());
					// 订单状态(1-未开始；2-开始服务；3-服务完成；10-订单取消；20-申请退款;21-完成退款；30-已结算)
					vo.setCreateTime(bo.getCreateTime());// 订单创建时间

					vo.setRechargeType(bo.getRechargeType()); // 方式（1-微信支付；2-余额支付）
					vo.setOrderIsevaluate(bo.getOrderIsevaluate()); // 是否评价(1-是；2-否)
					vo.setOrderIsreward(bo.getOrderIsreward()); // 是否打赏(1-是；2-否)
					vo.setExamineStatus(bo.getExamineStatus());// 退款申请审核（系统管理员）（0-不用审核；1-等待审核；2-通过；3-不通过）

					vos.add(vo);
				}
			}

			Map<String, Object> mape = new HashMap<String, Object>();
			Map<String, Object> tmape = new HashMap<String, Object>();
			tmape.put("dataList", vos);
			tmape.put("pageData", newuses.getPageData());
			tmape.put("totalData", newuses.getTotalData());
			tmape.put("endPage", newuses.getEndPage());
			tmape.put("curPage", newuses.getCurPage());
			tmape.put("totalPage", newuses.getTotalPage());
			mape.put("lis", tmape);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, mape);
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 获取用户定位地址后的小区列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/val", method = RequestMethod.POST)
	public Map<String, Object> villageList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 是否为空参数校验
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "lat", "lng" }, map);
			if (isNotNull) {
				// 查找 500 米以内的小区
				double lat = Double.valueOf(map.get("lat").toString());
				double lng = Double.valueOf(map.get("lng").toString());
				LOGGER.debug("get lat = " + lat + "=== get lng = " + lng);
				// 该值从系统配置中获取
				// 查询结算比例 washerProportion
				Map<String, Object> rMap = new HashMap<String, Object>();
				rMap.put("dictName", "nearbyDistrict");

				SysConfigDictionary c = dictionaryService.detail(rMap);
				long raidus = Long.valueOf(c.getDictValue());
				Map<String, Object> tmap = LatitudeLontitudeUtil.loadGeoSquare(lat, lng, raidus);
				List<BussUcenterArea> newuses = bussUcenterAreaService.findListByLatLng(tmap);
				if (newuses != null && newuses.size() > 0) {
					Map<String, Object> maps = new HashMap<String, Object>();
					maps.put("lis", newuses);
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, maps);

				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 获取用户输入关键字查询小区列表信息
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cvl", method = RequestMethod.POST)
	public Map<String, Object> checkVillageList(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 是否为空参数校验
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "areaName" }, map);
			if (isNotNull) {

				PageObject<BussUcenterArea> newuses = bussUcenterAreaService.Pagequery(map);
				if (newuses != null) {
					Map<String, Object> maps = new HashMap<String, Object>();
					maps.put("lis", newuses.getDataList());
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, maps);

				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 查询小区的详细地址
	 * 
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "aan", method = RequestMethod.POST)
	public Map<String, Object> getareasAllNames(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
			if (isNotNull) {
				long id = Long.valueOf(map.get("id").toString());
				BussUcenterArea are = areaService.findAreaInfo(id);
				if (are != null) {
					String name = are.getName();
					Map<String, Object> mape = new HashMap<String, Object>();
					mape.put("name", name);
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, mape);
				} else {
					resultMap.put("code", ReturnMsgTips.FAILAD_CODE);
					resultMap.put("msg", ReturnMsgTips.FAILAD_CODE_MSG);
					resultMap.put("data", "");
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			LOGGER.error("服务器异常!", e);
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}

		LOGGER.info("返回参数 ：{}", JSONObject.toJSON(resultMap));
		return resultMap;
	}

	/**
	 * 查询订单详情
	 * 
	 * @param request
	 * @param respons
	 * @param map
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/oi", method = RequestMethod.POST)
	public Map<String, Object> orderInfo(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 是否为空参数校验
		boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
		if (isNotNull) {
			Map<String, Object> rmaps = new HashMap<String, Object>();
			map.put("userId", getSessionUserId());
			BussUserOrder newuses = bussUserOrderService.detail(map);
			if (newuses != null) {
				String projectId = newuses.getProjectId();
				Map<String, Object> tMap = new HashMap<String, Object>();
				tMap.put("id", projectId);

				BussServiceProject pro = projectService.detail(tMap);
				if (pro != null) {
					newuses.setProjectDesc(pro.getProjectDesc());
					newuses.setProjectName(pro.getProjectName());
				}
				int areaId = newuses.getAreaId();
				String name = ucenterAreaService.getAreaName(areaId);
				newuses.setAreaAddress(name + newuses.getAreaAddress());
				// 查询订单评论信息
				Map<String, Object> rMap = new HashMap<String, Object>();
				rMap.put("orderId", newuses.getId());
				BussUserComment cbus = commentService.detail(rMap);
				if (cbus != null) {
					rmaps.put("comment", cbus);
				}
				rmaps.put("order", newuses);

				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, rmaps);
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, null);
			}
		} else {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
		}

		return resultMap;
	}

	/**
	 * 用户退款申请、针对 同一个洗车工每个月只有三次审核的名额仅限判断
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/rfs", method = RequestMethod.POST)
	public Map<String, Object> refunds(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 订单状态(1-未开始；2-开始服务；3-服务完成；10-订单取消；20-申请退款;21-完成退款；30-已结算)
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
			if (isNotNull) {
				long userId = getSessionUserId();
				map.put("userId", userId);
				int rs = bussUserOrderService.modifyRefunds(map);
				if (rs == 0) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else if (rs == -1) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_REFUNDS_ORDER_CODE, false);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		return resultMap;
	}

	/**
	 * 用户取消订单
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/cco", method = RequestMethod.POST)
	public Map<String, Object> cancelOrder(HttpServletRequest request, HttpServletResponse respons,
			@RequestParam Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		// 订单状态(1-未开始；2-开始服务；3-服务完成；10-订单取消；20-申请退款;21-完成退款；30-已结算)
		try {
			boolean isNotNull = ObjectUtil.checkObjectFile(new String[] { "id" }, map);
			if (isNotNull) {
				long userId = getSessionUserId();
				map.put("userId", userId);
				boolean rs = bussUserOrderService.cancelOrder(map);
				if (rs) {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.SUCCESSFUL_CODE, true);
				} else {
					resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_NULL_RESULT_CODE, false);
				}
			} else {
				resultMap = ReturnRsUtils.resultBackString(ParameterEunm.ERROR_PARAMS_CODE, null);
			}
		} catch (Exception e) {
			resultMap = ReturnRsUtils.resultBackString(ParameterEunm.FAILED_CODE, null);
		}
		return resultMap;
	}

	/**
	 * 添加用户车辆信息
	 * 
	 * @param orderVo
	 */
	public BussUserCars createCarInfo(Map<String, Object> map) {

		try {
			BussUserCars buc = new BussUserCars();
			buc.setUserId(Long.valueOf(map.get("userId").toString()));
			buc.setAreaId(Integer.valueOf(map.get("areaId").toString())); // 预约服务地址
			// 预约服务地址
			if (map.get("areaAddress") != null) {
				buc.setAreaAddress(map.get("areaAddress").toString());
			}

			buc.setCarCode(map.get("carCode").toString()); // 车牌号

			// 车辆图片
			if (map.get("carBrand") != null) {
				buc.setCarBrand(map.get("carBrand").toString());
			}

			// 车辆图片
			if (map.get("carColor") != null) {
				buc.setCarCode(map.get("carColor").toString());
			}

			// 车辆图片
			if (map.get("carImages") != null) {
				buc.setCarImages(map.get("carImages").toString());
			}

			buc.setLinkMan(map.get("linkMan").toString()); // 联系人
			buc.setLinkTel(map.get("linkTel").toString()); // 联系电话
			buc.setCreateTime(DateUtils.getNowDate());// 订单创建时间',
			buc.setCreateUserId(Long.valueOf(map.get("userId").toString()));// 创建用户id',
			BussUserCars nbuc = bussUserCarsService.add(buc);
			if (nbuc != null) {
				return nbuc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
